package com.softstao.guoyu.ui.activity.sale;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;

import com.softstao.guoyu.R;
import com.softstao.guoyu.base.BaseActivity;
import com.softstao.guoyu.global.SharePreferenceManager;
import com.softstao.guoyu.model.shop.Goods;
import com.softstao.guoyu.model.shop.ShopIndex;
import com.softstao.guoyu.mvp.interactor.shop.GoodsInteractor;
import com.softstao.guoyu.mvp.presenter.shop.GoodsPresenter;
import com.softstao.guoyu.mvp.viewer.shop.GoodsListViewer;
import com.softstao.guoyu.ui.adapter.ChooseProductAdapter;
import com.softstao.softstaolibrary.library.widget.CustomScrollerView;
import com.softstao.softstaolibrary.library.widget.FullyGridLayoutManager;
import com.softstao.softstaolibrary.library.widget.LZToast;
import com.wangjie.androidinject.annotation.annotations.mvp.AIPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by vaikus on 2017/7/11.
 */

public class ChooseProductActivity extends BaseActivity implements GoodsListViewer, ChooseProductAdapter.GoodsListener {

    @AIPresenter(presenter = GoodsPresenter.class, interactor = GoodsInteractor.class)
    GoodsPresenter presenter;

    @BindView(R.id.list_view)
    RecyclerView listView;
    @BindView(R.id.loading)
    RelativeLayout loading;
    @BindView(R.id.scrollView)
    CustomScrollerView scrollView;
    private ChooseProductAdapter goodsAdapter;
    private List<Goods> goodsList = new ArrayList<>();

    @Override
    public int _ContentView() {
        return R.layout.activity_choose_product;
    }

    @Override
    public void initView() {
        initTitle("选择产品");
        scrollView.setOnScrollChangedListener(this);

        goodsAdapter = new ChooseProductAdapter(goodsList);
        goodsAdapter.setGoodsListener(this);
        listView.setAdapter(goodsAdapter);
        listView.setLayoutManager(new FullyGridLayoutManager(this,1));
    }

    @OnClick(R.id.next_btn)
    public void onViewClicked() {
        if (SharePreferenceManager.getInstance().getChooseGoods()!=null&&SharePreferenceManager.getInstance().getChooseGoods().size()!=0){
            startActivity(new Intent(context, AddRetailActivity.class));
            finish();
        }else {
            LZToast.getInstance(context).showToast("请先选择商品");
            return;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        findGoods();
        SharePreferenceManager.getInstance().setChooseGoods(null,0);
    }

    @Override
    public void onLoad() {
        super.onLoad();
        findGoods();
    }

    @Override
    public void findGoods() {
        loading.setVisibility(View.VISIBLE);
        presenter.getGoodsList(currentPage, SharePreferenceManager.getInstance().getAgentId(), "");
    }

    @Override
    public void getGoodsList(ShopIndex shopIndex) {
        loading.setVisibility(View.GONE);
        if (shopIndex != null) {
            if (shopIndex.getProductList() != null && shopIndex.getProductList().size() != 0) {
                if (currentPage == 1) {
                    goodsList.clear();
                }
                goodsList.addAll(shopIndex.getProductList());
                goodsAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    protected void error(String msg) {
        super.error(msg);
        loading.setVisibility(View.GONE);
    }

    @Override
    public void subtractPrice(int position, int preNumber, int number) {
        SharePreferenceManager.getInstance().setChooseGoods(goodsList.get(position),-1);
    }

    @Override
    public void addPrice(int position) {
        SharePreferenceManager.getInstance().setChooseGoods(goodsList.get(position),1);
    }
}
